package com.voicechat.firstmet.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.BuyVIPActivity;
import com.voicechat.firstmet.activity.GoldDepositsActivity;
import com.voicechat.firstmet.activity.PersonalHomepageActivity;
import com.voicechat.firstmet.adapter.LookMeAdapter;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.bean.FansAndLike;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment
 * @ClassName: LookMeFragment
 * @Description: 看过我 和我看过的页面
 * @Author: ljp
 * @CreateDate: 2018/10/11 16:35
 * @Version: 1.0
 */
public class LookMeFragment extends BaseFragment {
    @BindView(R.id.tv_open_vip)
    TextView tvOpenVip;
    @BindView(R.id.linear_who_lookme)
    LinearLayout linearWhoLookme;
    @BindView(R.id.recycler_lists)
    RecyclerView recyclerLists;
    @BindView(R.id.linear_who_lookme_list)
    LinearLayout linearWhoLookmeList;
    Unbinder unbinder;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.linear_layout)
    LinearLayout linearLayout;

    private String flag;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String id;
    private String deviceInfo;
    private String vType;
    private ArrayList<FansAndLike.PageBean.ListBean> listBeans;
    private LookMeAdapter lookMeAdapter;
    private int page;

    public static LookMeFragment newInstance(String flag, String vType) {
        LookMeFragment lookMeFragment = new LookMeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("flag", flag);
        bundle.putString("vType", vType);
        lookMeFragment.setArguments(bundle);
        return lookMeFragment;
    }

    @Override
    protected int loadLayout() {
        return R.layout.fragment_look_me;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        page = 1;
        listBeans = new ArrayList<>();
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        id = mySeleInfo.getId();
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        flag = getArguments().getString("flag");
        vType = getArguments().getString("vType");//判断是否是会员 // 会员等级 0非会员 1月会员 2年会员
        if (flag.equals("LOOKME")) {
            if (vType.equals("0")) {
                smartRefresh.setVisibility(View.GONE);
                linearWhoLookme.setVisibility(View.VISIBLE);
            } else {
                linearWhoLookme.setVisibility(View.GONE);
                smartRefresh.setVisibility(View.VISIBLE);
                setHttpDataMeLook();
            }
        } else {
            setHttpDataLookMe();
        }
        lookMeAdapter = new LookMeAdapter(context, listBeans, R.layout.item_look_me);
        recyclerLists.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerLists.setAdapter(lookMeAdapter);
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                if (flag.equals("LOOKME")) {
                    setHttpDataMeLook();
                } else {
                    setHttpDataLookMe();
                }
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (flag.equals("LOOKME")) {
                    setHttpDataMeLook();
                } else {
                    setHttpDataLookMe();
                }
                smartRefresh.finishLoadMore(10000);
            }
        });
        tvOpenVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, BuyVIPActivity.class);
                intent.putExtra("data","");
                startActivity(intent);
               // finish();
              /*  Uri uri = Uri.parse(HTTPAPI.URL_MEMBER+"id="+mySeleInfo.getNumber());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);*/
            }
        });

        lookMeAdapter.setItemOnClick(new LookMeAdapter.ItemOnClick() {
            @Override
            public void setIconOnClick(FansAndLike.PageBean.ListBean listBean) {
                Intent intent = new Intent(activity, PersonalHomepageActivity.class);
                intent.putExtra("anchorId", listBean.getUserId());
                startActivity(intent);
            }
        });


    }

    /**
     * @return
     * @method
     * @description 谁看过我数据请求
     * @date: 2018/10/30 15:03
     * @author: ljp
     */
    private void setHttpDataMeLook() {
        MeInfoService.meLook(token, deviceInfo, id, page + "").subscribe(fansAndLike -> {
            if (fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = fansAndLike.getPage().getCount() / fansAndLike.getPage().getPageSize() +
                        (fansAndLike.getPage().getCount() % fansAndLike.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        listBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if(fansAndLike.getPage().getList() != null) {
                        listBeans.addAll(fansAndLike.getPage().getList());
                        lookMeAdapter.notifyDataSetChanged();
                        smartRefresh.setVisibility(View.VISIBLE);
                    }else {
                        linearLayout.setVisibility(View.VISIBLE);
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(fansAndLike.getMsg());
            }

        }, throwable -> {

        });

    }

    /**
     * @return
     * @method
     * @description 我看过谁数据请求
     * @date: 2018/10/30 15:03
     * @author: ljp
     */
    private void setHttpDataLookMe() {
        MeInfoService.lookMe(token, deviceInfo, id, page + "").subscribe(fansAndLike -> {
            if (fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = fansAndLike.getPage().getCount() / fansAndLike.getPage().getPageSize() +
                        (fansAndLike.getPage().getCount() % fansAndLike.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        listBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if(fansAndLike.getPage().getList() != null) {
                        listBeans.addAll(fansAndLike.getPage().getList());
                        lookMeAdapter.notifyDataSetChanged();
                        smartRefresh.setVisibility(View.VISIBLE);
                    }else {
                        linearLayout.setVisibility(View.VISIBLE);
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(fansAndLike.getMsg());
            }

        }, throwable -> {

        });

    }

    /**
     * @return
     * @method
     * @description 跳往开通会员页面
     * @date: 2018/10/11 20:22
     * @author: ljp
     */

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
